<?php

function read_CSV_and_insert_into_DB(){
	require_once(dirname(__FILE__) . "../../configs/configs.php");
	global $conn;
		
	$databasetable_product = "tbl_product";
	$databasetable_seller = "tbl_seller";
	$databasetable_offer = "tbl_offer";
	$databasetable_product_offer = "tbl_product_offer";
	$databasetable_seller_product = "tbl_seller_product";
	
	$fieldseparator = ",";
	$lineseparator = "\n";	
	$csvfile = "../admin/upload/csv/sampleData.csv";
	/********************************/
	/* Would you like to add an ampty field at the beginning of these records?
	/* This is useful if you have a table with the first field being an auto_increment integer
	/* and the csv file does not have such as empty field before the records.
	/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
	/* This can dump data in the wrong fields if this extra field does not exist in the table
	/********************************/
	$addauto = 1;
	/********************************/
	/* Would you like to save the mysql queries in a file? If yes set $save to 1.
	/* Permission on the file should be set to 777. Either upload a sample file through ftp and
	/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
	/********************************/
	$save = 1;
	$outputfile = "output.sql";
	/********************************/


	if(!file_exists($csvfile)) {
		echo "ERROR.File not found. Make sure you specified the correct path.\n";
		exit;
	}

	$file = fopen($csvfile,"r");

	if(!$file) {
		echo "ERROR.Error opening data file.\n";
		exit;
	}

	$size = filesize($csvfile);

	if(!$size) {
		echo "ERROR.File is empty.\n";
		exit;
	}

	$csvcontent = fread($file,$size);

	//echo('Content--->'. $csvcontent);

	fclose($file);

	$lines = 0;
	$queries = "";
	$linearray = array();

	foreach(explode($lineseparator,$csvcontent) as $line) {

		//echo $line;
		$lines++;
		$line = trim($line," \t");	
		$line = str_replace("\r","",$line);
		
		/************************************
		This line escapes the special character. remove it if entries are already escaped in the csv file
		************************************/
		$line = str_replace("'","\'",$line);
		/*************************************/

		//echo "len-->".explode(',',$line);
		//echo $line;

		$CSV_arr = explode($fieldseparator,$line);			
		//splitting main CSV_arr into three sub arrays for respective tables tbl_products,tbl_seller and tbl_offer		 
		//array_slice(array_namr,start_range,and_its_length)
		$tbl_product_arr =array_slice($CSV_arr,0,9);
		$tbl_seller_arr =array_slice($CSV_arr,9,5);
		$tbl_offer_arr =array_slice($CSV_arr,14,4);	

		$tbl_product_line = implode("','",$tbl_product_arr);
		$tbl_seller_line = implode("','",$tbl_seller_arr);
		$tbl_offer_line = implode("','",$tbl_offer_arr);

		if($addauto)
		{
			$query1 = "insert into $databasetable_product values('','$tbl_product_line')";								
			mysql_query($query1, $conn);		    	
			$product_inserted_id =  mysql_insert_id();							
			//echo ('QUERY-->'. $query1). "\n";

			$query2 = "insert into $databasetable_seller values('','$tbl_seller_line')";			
			mysql_query($query2, $conn);
			$seller_inserted_id =  mysql_insert_id();
			//echo ('QUERY-->'. $query2). "\n";

			$query3 = "insert into $databasetable_offer values('','$tbl_offer_line')";			
			mysql_query($query3, $conn);
			//echo ('QUERY-->'. $query3). "\n";
			$offer_inserted_id =  mysql_insert_id();
			
			
			$query4 = "insert into $databasetable_seller_product values($seller_inserted_id,$product_inserted_id)";			
			mysql_query($query4, $conn);
			
			
			$query5 = "insert into $databasetable_product_offer values($product_inserted_id,$offer_inserted_id)";			
			mysql_query($query5, $conn);
		}
		
		//$queries = $query1."\n".$query2."\n".$query3;
		
	}
	@mysql_close($conn);
	/*if($save) {
		//echo "WRITING................";
		if(!is_writable($outputfile)) {
			echo "ERROR.File is not writeable, check permissions.\n";
		}
		
		else {
			$file2 = fopen($outputfile,"w");
			
			if(!$file2) {
				echo "ERROR.Error writing to the output file.\n";
			}
			else {
				fwrite($file2,$queries);
				fclose($file2);
			}
		}
		
	}*/

	echo "SUCCESS. AROCITY database has been updated with recent CSV file !!!";	
}
?>
